package com.dsl.perfs;

import com.dsl.ast.EasyDSLBaseListener;
import com.dsl.ast.EasyDSLParser;
import org.antlr.v4.runtime.misc.NotNull;

import java.util.Stack;

public class EasyDSLListener extends EasyDSLBaseListener {

    private Stack<ExprNode> stacks = new Stack<>();

    @Override
    public void exitIdentifier(@NotNull EasyDSLParser.IdentifierContext ctx) {
        stacks.push(new ExprNode(ExprType.ID, ctx.getText(), null, null));
    }

    @Override
    public void exitAndEpr(@NotNull EasyDSLParser.AndEprContext ctx) {
        ExprNode right = stacks.pop();
        ExprNode left = stacks.pop();

        stacks.push(new ExprNode(ExprType.AND, null, left, right));
    }

    @Override
    public void exitOrEpr(@NotNull EasyDSLParser.OrEprContext ctx) {
        ExprNode right = stacks.pop();
        ExprNode left = stacks.pop();

        stacks.push(new ExprNode(ExprType.OR, null, left, right));
    }

    @Override
    public void exitLine(@NotNull EasyDSLParser.LineContext ctx) {
        super.exitLine(ctx);
    }

    @Override
    public void exitParenExpr(@NotNull EasyDSLParser.ParenExprContext ctx) {
        // DO NOTHING
    }

    public ExprNode getResult() {
        return stacks.peek();
    }

}
